#include "../include/client.h"
#include "../../command/command.h"

int main(int argc, char *argv[]){                                  
    // client.c 192.168.. 12345
    ARGS_CHECK(argc,3);
    int sockfd =socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    int ret =connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    //创建epoll
    int epfd=epoll_create(1);
    epollAdd(epfd,STDIN_FILENO);
    epollAdd(epfd,sockfd);
    char buf[4096] = {0}; 
    char dest_cmd[4096] = {0};
    char real_cmd[4096] = {0};
    char user_buf[1024] = {0};
    char hash_name[4096] = {0};
    char user_name[1024] = {0};
    int flag = 0;
    struct epoll_event readyset[1024];

    printf("--------------\n");
    // 登录
    for(int i = 0;i<2;++i){
        recv(sockfd,user_buf,sizeof(user_buf),0);
        printf("%s\n",user_buf);
        bzero(user_buf,sizeof(user_buf));
        scanf("%s",user_buf);
        if(i == 0){
            strcpy(user_name,user_buf);
        }
        send(sockfd,user_buf,strlen(user_buf),MSG_NOSIGNAL);
    }
    bzero(user_buf,sizeof(user_buf));
    recv(sockfd,user_buf,sizeof(user_buf),0);
    printf("%s\n",user_buf);
    // 指令
    while(1){
        printf("%s:",user_name); // >>: LS 
        fflush(stdout);
        int readynum =epoll_wait(epfd,readyset,1024,-1);
        for(int i =0;i < readynum;i++){
            if(readyset[i].data.fd == STDIN_FILENO){
                bzero(buf,sizeof(buf));
                bzero(dest_cmd,sizeof(dest_cmd));
                bzero(real_cmd,sizeof(real_cmd));
                bzero(hash_name,sizeof(hash_name));
                read(STDIN_FILENO,buf,sizeof(buf));
                int ret = splitCommand(buf,dest_cmd);
                if(ret == -1){
                    continue;
                }
                delSpace(dest_cmd,real_cmd);
                sendCmd(sockfd,real_cmd,ret);
                if(ret == PUTS){
                    real_cmd[strlen(real_cmd)-1] = '\0';
                    int fret = fileContentHash_sha256(real_cmd,hash_name);
                    if(fret == -1){
                        sendCmd(sockfd,"-1",-1);
                        break;
                    }
                    int speedTransFlag = 0;
                    bzero(buf,sizeof(buf));
                    sendCmd(sockfd,hash_name,ret);
                    recvCmd(sockfd,buf,&speedTransFlag);
                    if(speedTransFlag == -1){
                        printf("极速秒传成功!\n");
                        break;
                    }
                    sendFile(sockfd,real_cmd,real_cmd,ret);
                }
            }
            else{
                printf("--------------\n");
                bzero(&flag,sizeof(flag));
                bzero(buf,sizeof(buf));
                recvCmd(sockfd,buf,&flag);
                if(flag == GETS){
                    bzero(buf,sizeof(buf));
                    recvFile(sockfd,buf,&flag);
                }else if(flag == 0){
                    close(sockfd);
                    return 0;
                }
                printf("%s\n",buf);
            }       
        }
    }
    return 0;
}

